package com.yc.web;

import com.yc.dao.DBHelper;
import com.yc.bean.Shoes;
import com.yc.web.model.PageBean;

import java.util.List;

public class ResfoodBiz {
    public PageBean findByPage (PageBean pageBean) throws Exception {
        List<Shoes> dataset = this.findByPage(pageBean.getPageno(),pageBean.getPagesize(),pageBean.getSortby(),pageBean.getSort());
        long total = this.countAll();

        pageBean.setTotal(total);
        pageBean.setDataset(dataset);
        //其它的分页数据
        //计算总页数
        long totalPages = total%pageBean.getPagesize()==0?total/pageBean.getPagesize():total/ pageBean.getPagesize()+1;
        pageBean.setTotalpages((int)totalPages);
        //上一页页号的计算
        if (pageBean.getPageno()<=1){
            pageBean.setPre(1);
        }else {
            pageBean.setPre(pageBean.getPageno()-1);
        }

        //下一页页号的计算
        if (pageBean.getPageno()==totalPages){
            pageBean.setNext((int)totalPages);
        }else {
            pageBean.setNext(  pageBean.getPageno()+1  );
        }
        return pageBean;
    }
    /*
    查询resfood的记录条数
    */
    private long countAll() throws Exception {
        String sql = "select count(sid) count from shoes";
        DBHelper db = new DBHelper();
        long l = (long) db.select(sql).get(0).get("count");
        return l;
    }
   /*
   查询当前pageno这一页的数据
    pageno  当前页数
    pagesize 每页有多少条
    */
    private List<Shoes> findByPage(int pageno,int pagesize,String sortby,String sort) throws Exception {
//        String sql = "select * from resfood order by ? ? limit ?,?";
        String sql = "select Shoes.sid,sbrand,sgender,ssize,scolor,style,sprice,sdescription,sstockquantity,photourl,ismain FROM Shoes JOIN ShoePhotos ON Shoes.sid = ShoePhotos.sid JOIN ShoeStyle ON Shoes.sstyle=ShoeStyle.styleid WHERE ismain='true' order by "+sortby+" "+sort+" limit ?,?";
        DBHelper db = new DBHelper();
        int start = (pageno-1)*pagesize;
//        List<Resfood> list = db.select(Resfood.class,sql,sortby,sort,start,pagesize);
        List<Shoes> list = db.select(Shoes.class,sql,start,pagesize);
        return list;
    }
}
